package org.intrasearch.core.io;

import com.google.common.collect.ImmutableSet;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;

public class ImmutableSetExternalizableProvider<E> implements ExternalizableProvider<ImmutableSet<E>>
{
    public void writeExternal(ImmutableSet<E> object, ObjectOutput output) throws IOException
    {
        output.writeInt(object.size());
        for (E e : object)
        {
            output.writeObject(e);
        }
    }

    public ImmutableSet<E> readExternal(ObjectInput input) throws IOException, ClassNotFoundException
    {
        int size = input.readInt();
        ArrayList<E> list = new ArrayList<E>();
        for (int i = 0; i < size; i++)
        {
            list.add((E) input.readObject());
        }
        return ImmutableSet.copyOf(list);
    }
}
